package com.inei.cie.utilitarios;

import java.util.List;

import android.content.Context;
import android.graphics.Color;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.inei.cie.R;
import com.inei.cie.dao.PrincipalDao;
import com.inei.cie.entidades.PCar;

public class ListadoLocalesAdapter extends BaseAdapter {

	Context context;
	List<PCar> lista;

	public ListadoLocalesAdapter(Context context, List<PCar> lista) {
		this.context = context;
		this.lista = lista;
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return lista.size();
	}

	@Override
	public Object getItem(int pos) {
		// TODO Auto-generated method stub
		return lista.get(pos);
	}

	@Override
	public long getItemId(int pos) {
		// TODO Auto-generated method stub
		return Long.parseLong(lista.get(pos).getId_local());
	}

	@Override
	public View getView(int pos, View view, ViewGroup group) {
		// TODO Auto-generated method stub
		View item = view;
		ViewHolder holder;
		if (item == null) {
			LayoutInflater inflater = (LayoutInflater) context
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			item = inflater.inflate(R.layout.carga_trabajo_item, null);
			holder = new ViewHolder();
			holder.tvIdLocal = (TextView) item.findViewById(R.id.tvIdLocal);
			holder.tvCompleto = (TextView) item.findViewById(R.id.tvCompleto);
			holder.tvEnviado = (TextView) item.findViewById(R.id.tvEnviado);
			item.setTag(holder);
		} else {
			holder = (ViewHolder) item.getTag();
		}
		PCar obj = lista.get(pos);
		holder.tvIdLocal.setText(obj.getId_local());
		PrincipalDao principal = new PrincipalDao(context);
		Log.i("com.inei.cie",
				obj.getId_local() + " - " + principal.cap1(obj.getId_local()));

		if (obj.getSwCompleto() == -1) {
			holder.tvCompleto.setText("Sin registro");
			holder.tvCompleto.setTextColor(Color.BLACK);
		} else {
			if (obj.getSwCompleto() == 0) {
				holder.tvCompleto.setText("Incompleto");
				holder.tvCompleto.setTextColor(Color.RED);
			} else {
				if (principal.car(obj.getId_local())
						&& principal.cap1(obj.getId_local())
						&& principal.cap2(obj.getId_local())
						&& principal.cap3(obj.getId_local())
						&& principal.cap4(obj.getId_local())
						&& principal.cap5(obj.getId_local())
						&& principal.cap6(obj.getId_local())
						&& principal.cap7(obj.getId_local())
						&& principal.cap8(obj.getId_local())) {
					holder.tvCompleto.setText("Completo");
					holder.tvCompleto.setTextColor(Color.GREEN);
				} else {
					holder.tvCompleto.setText("Incompleto");
					holder.tvCompleto.setTextColor(Color.RED);
				}
			}
		}
		if (obj.getId_local().equals("000019")) {
			Log.i("Car", principal.car(obj.getId_local()) + "");
			Log.i("Cap1", principal.cap1(obj.getId_local()) + "");
			Log.i("Cap2", principal.cap2(obj.getId_local()) + "");
			Log.i("Cap3", principal.cap3(obj.getId_local()) + "");
			Log.i("Cap4", principal.cap4(obj.getId_local()) + "");
			Log.i("Cap5", principal.cap5(obj.getId_local()) + "");
			Log.i("Cap6", principal.cap6(obj.getId_local()) + "");
			Log.i("Cap7", principal.cap7(obj.getId_local()) + "");
			Log.i("Cap8", principal.cap8(obj.getId_local()) + "");
		}

		holder.tvEnviado.setText(obj.getSwEnviado() == 1 ? "Enviado"
				: "No enviado");
		if (obj.getSwEnviado() == 0) {
			holder.tvEnviado.setTextColor(Color.RED);
		}
		return item;
	}

	class ViewHolder {
		TextView tvIdLocal;
		TextView tvCompleto;
		TextView tvEnviado;
	}

}
